﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using EngineOfEvermore.Common;

namespace EngineOfEvermore.Scene.Visual
{
    public abstract class VisualComponentChild : Drawable, IObjectWithOptionalName
    {
        #region Variables
        VisualComponent _owner;
        String _name;
        readonly bool _needsUpdate;
        #endregion

        #region Events
        public event Action<TryRenameEventArgs> TryRenameObject;
        #endregion

        #region Properties
        public VisualComponent Owner
        {
            get { return _owner; }
        }

        public String Name
        {
            get { return _name; }
            set 
            {
                if ( value != null && TryRenameObject != null )
                {
                    TryRenameEventArgs e = new TryRenameEventArgs( value );
                    TryRenameObject( e );

                    if ( e.HasBeenAborted )
                    {
                        throw new ArgumentException( "An object with the name " + value +
                            " is already present in the parent container." );
                    }
                }

                _name = value;
            }
        }

        // TODO: Don't need all visual component child classes updates?
        public bool NeedsUpdate
        {
            get { return _needsUpdate; }
        }
        #endregion

        #region Constructor
        protected VisualComponentChild( bool needsUpdate )
        {
            _needsUpdate = needsUpdate;
        }
        #endregion

        /// <summary>
        /// Called when this child is added to a VisualComponent. 
        /// </summary>
        /// <remarks>
        /// When overriding call base at the BEGINNING of the overriding method.
        /// </remarks>
        /// <param name="component">The new owner</param>
        public virtual void OnAddToVisualComponent( VisualComponent component )
        {
            Debug.Assert( _owner == null );

            _owner = component;
        }

        /// <summary>
        /// Called when this child is removed from a VisualComponent.
        /// </summary>
        /// <remarks>
        /// When overriding call base at the END of the overriding method.
        /// </remarks>
        /// <param name="component"></param>
        public virtual void OnRemoveFromVisualComponent( VisualComponent component )
        {
            Debug.Assert( _owner == component );

            _owner = null;
        }

        public virtual void OnSiblingAdded( VisualComponentChild sibling )
        {
        }

        public virtual void OnSiblingRemoved( VisualComponentChild sibling )
        { 
        }

        public virtual void Update( LogicUpdateEventArgs e )
        { 
        }

        public VisualComponentChild Clone()
        {
            var clone = _clone();
            clone.Name = this.Name;
            return clone;
        }

        protected abstract VisualComponentChild _clone();
    }
}
